package com.wits.qso.service;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wits.platform.service.BaseService;
import com.wits.qso.dao.CListDao;
import com.wits.qso.entity.CList;
import com.wits.qso.entity.Course;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.entity.MaterialH;

/**
 * @author Ethan Lee
 */
@Component
public class CListService extends BaseService<CList, String>
{
	protected static final Logger logger = LoggerFactory.getLogger( CListService.class );

	private CListDao cListDao = null;
	@Autowired
	private CourseService courseService = null;
	@Autowired
	private MaterialdService materialdService = null;
	@Autowired
	private MaterialHService materialHService = null;
	
	/**
	 * 注入班级dao
	 * 
	 * @param clsDao
	 */
	@Autowired
	public void setCListDao(CListDao cListDao) {
		this.baseDao = cListDao;
		this.cListDao = cListDao;
	}

	public String getCListTree() {
		CList root = cListDao.findRoot();
		if (null == root) {
			return "";
		} else {
			StringBuilder builder = new StringBuilder();
			builder.append( "{text: '" ).append( root.getName() ).append( "'" );
			builder = traverse( root, builder );

			return builder.toString();
		}
	}
	/**
	 * 根据传入的课程Id查询课程下所有的课件资源。
	 * @return
	 */
	public String getCListTree(String courseId) {
		Course course = courseService.getOne(courseId);
		CList root=course!=null?course.getListId():null;
		if (null == root) {
			return "";
		} else {
			StringBuilder builder = new StringBuilder();
			builder.append( "{text: '" ).append( root.getName() ).append( "'" );
			builder = courseMaterial( course, builder );
			return builder.toString();
		}
	}
	/**
	 * 是否有根节点
	 * @return
	 */
	public boolean hasRootNode() {
		return null != cListDao.findRoot();
	}
	/**
	 * 将根节点与课程结合起来
	 * @param cList
	 * @param builder
	 * @return
	 */
	protected StringBuilder traverse(CList cList, StringBuilder builder) {
		List<CList> nodes = cListDao.findNode( cList );
		if (0 == nodes.size()) {
			List<Course> courses = courseService.findByLst( cList );
			if (0 != courses.size()) {
				builder.append( ", children: [" );
				int i=0;
				for(Course course : courses) {
					i++;
					Iterator<MaterialD> materialDIter = materialdService.findByCourse( course ).iterator();
					builder.append( "{text:'" ).append( course.getTitle() ).append( "', children: [" );
					while(materialDIter.hasNext()) {
						MaterialD materialD = materialDIter.next();
//						MaterialH materialH = materialHService.getOne( materialD.getMtrhId() );
//						builder.append( "{text:'" ).append( materialH.getCouName() ).append( "', children: [" );
						builder.append( "{text:'" ).append( materialD.getPath() ).append("',");
						builder.append( "id:'" ).append( materialD.getId()).append( "'}" );
						
						if (materialDIter.hasNext()) { builder.append( "," ); }
					}
					builder.append( "]}" );
					if(i<courses.size()){ builder.append( "," ); }
				}
				builder.append( "]" );
			}
			
			return builder.append( "}" );
		} else {
			builder.append( ", children: [" );

			Iterator<CList> iterator = nodes.iterator();
			while (iterator.hasNext()) {
				CList node = iterator.next();
				builder.append( "{text:'" ).append( node.getName() ).append( "'" );
				builder = traverse( node, builder );
				if (iterator.hasNext()) { builder.append( "," ); }
			}

			builder.append( "]}" );
			return builder;
		}
	}
	/**
	 * 查询课程下的课件
	 * @param courseId
	 * @param builder
	 * @return
	 */
	private StringBuilder courseMaterial(Course course, StringBuilder builder) {
		builder.append(", children: [");
		Iterator<MaterialD> materialDIter = materialdService.findByCourse(
				course).iterator();
		builder.append("{text:'").append(course.getTitle())
				.append("', children: [");
		while (materialDIter.hasNext()) {
			MaterialD materialD = materialDIter.next();
			builder.append("{text:'").append(materialD.getPath()).append("',");
			builder.append("id:'").append(materialD.getId()).append("'}");

			if (materialDIter.hasNext()) {
				builder.append(",");
			}
		}
		builder.append("]}]}");
		return builder;
	}
}
